﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#include "CellStableHeaders.h"

#include "CellLogger.h"
#include "CellLoggerManager.h"
#include "CellString.h"

namespace cell
{

//-----------------------------------------------------------------------
Logger::Logger(const String& name, bool debuggerOuput, bool suppressTerminal)
	: mLogLevel(LL_NORMAL)
	, mDebugOut(debuggerOuput)
	, mFileLog(NULL)
	, mSuppressTerminal(suppressTerminal)
	, mTimeStamp(true)
	, mLogName(name)
{
	if (mSuppressTerminal)
	{
		mFileLog = fopen(name.c_str(), "wt");
	}
	else
	{
		mfpLog.open(name.c_str());
	}
}
//-----------------------------------------------------------------------
Logger::~Logger()
{
	CELL_LOCK_AUTO_MUTEX
	if (mSuppressTerminal)
	{
		fclose(mFileLog);
	}
	else
	{
		mfpLog.close();
	}
}
//-----------------------------------------------------------------------
void Logger::log(const String& message, LogMessageLevel lml, bool maskDebug)
{
	CELL_LOCK_AUTO_MUTEX
	if ((mLogLevel + lml) >= CELL_LOG_THRESHOLD)
	{
		for (mtLogListener::iterator i = mListeners.begin(); i != mListeners.end(); ++i)
			(*i)->messageLogged(message, lml, maskDebug, mLogName);

		// Write time into log
		if (mSuppressTerminal)
		{
			if (mTimeStamp)
			{
				struct tm *pTime;
				time_t ctTime;
				time(&ctTime);
				pTime = localtime(&ctTime);
			#if CELL_PLATFORM == CELL_PLATFORM_WIN32
				fprintf_s(mFileLog, "%02d:%02d:%02d  ", pTime->tm_hour, pTime->tm_min, pTime->tm_sec);
			#else
				fprintf(mFileLog, "%02d:%02d:%02d  ", pTime->tm_hour, pTime->tm_min, pTime->tm_sec);
			#endif
			}
		#if CELL_PLATFORM == CELL_PLATFORM_WIN32
			fprintf_s(mFileLog, "%s\n", message.c_str());
		#else
			fprintf(mFileLog, "%s\n", message.c_str());
		#endif

			// Flush log data
			fflush(mFileLog);
		}
		else
		{
			if (mDebugOut && !maskDebug)
				std::cerr << message << std::endl;

			if (mTimeStamp)
			{
				struct tm *pTime;
				time_t ctTime; time(&ctTime);
				pTime = localtime(&ctTime);
				mfpLog << std::setw(2) << std::setfill('0') << pTime->tm_hour
					<< ":" << std::setw(2) << std::setfill('0') << pTime->tm_min
					<< ":" << std::setw(2) << std::setfill('0') << pTime->tm_sec
					<< "  ";
			}
			mfpLog << message << std::endl;

			// Flush stcmdream to ensure it is written (incase of a crash, we need log to be up to date)
			mfpLog.flush();
		}
	}
}
//-----------------------------------------------------------------------
void Logger::setTimeStampEnabled(bool timeStamp)
{
	CELL_LOCK_AUTO_MUTEX
	mTimeStamp = timeStamp;
}
//-----------------------------------------------------------------------
void Logger::setDebugOutputEnabled(bool debugOutput)
{
	CELL_LOCK_AUTO_MUTEX
	mDebugOut = debugOutput;
}
//-----------------------------------------------------------------------
void Logger::setLogDetail(LoggingLevel ll)
{
	CELL_LOCK_AUTO_MUTEX
	mLogLevel = ll;
}
//-----------------------------------------------------------------------
void Logger::addListener(LogListener* listener)
{
	CELL_LOCK_AUTO_MUTEX
	mListeners.push_back(listener);
}
//-----------------------------------------------------------------------
void Logger::removeListener(LogListener* listener)
{
	CELL_LOCK_AUTO_MUTEX
	mListeners.erase(std::find(mListeners.begin(), mListeners.end(), listener));
}
//---------------------------------------------------------------------
Logger::Stream Logger::stream(LogMessageLevel lml, bool maskDebug) 
{
	return Stream(this, lml, maskDebug);
}

} // end namespace cell
